//
//  WBMainViewController.swift
//  传智微博
//
//  Created by 郑亚伟 on 16/11/3.
//  Copyright © 2016年 郑亚伟. All rights reserved.
//

import UIKit
//主控制器
class WBMainViewController: UITabBarController {
    override func viewDidLoad() {
        super.viewDidLoad()
        setupChildViewControllers()
        setupComposeButton()
    }
    
    
    //MARK: 设备支持方向 报错？？？？
//    override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask{
//        return .portrait;
//    }
    
    //MARK:-撰写按钮
    //FIXME:-没有实现
    // private能够保证方法私有，仅在当前对象被访问
    //@objc允许这个函数运行时通过OC的消息机制被调用
    @objc private  func composeStatus(){
        print("撰写微博")
        
        let vc = UIViewController()
        vc.view.backgroundColor = UIColor.red
        present(vc, animated: true, completion: nil)
    }
    
    //MARK: - 私有控件
    //懒加载后面要接: UIButton 否则报错
    private lazy var composeButton: UIButton = UIButton.cz_imageButton("tabbar_compose_icon_add", backgroundImageName: "tabbar_compose_button")
 
    
    //设置撰写按钮
   private func setupComposeButton() {
        tabBar.addSubview(composeButton)
        //强制转化
        let count: CGFloat = CGFloat((viewControllers?.count)!)
        let w = tabBar.bounds.size.width / count - 1// -1是为了处理容错点
        //注意这个坐标设置  CGRectInset正数向内缩进，负数向外扩展
        //tabbar左边和右边同时向里面缩进2 * w，中间的位置留给composeButton
        composeButton.frame = tabBar.bounds.insetBy(dx: 2 * w, dy: 0)
        //容错点 tabbar两个按钮之间存在点，如果不设置上面-1，可能会出现白色界面。解决思路是将撰写按钮的宽度变大
        print("按钮宽度：\(composeButton.bounds.width)")
       //因为composeStatus方法设置为私有方法，这里不可见，所以composeStatus要特殊处理。@objc private
        composeButton.addTarget(self, action:#selector(composeStatus), for: .touchUpInside)
      // composeButton.addTarget(self, action: #selector(<#T##@objc method#>), for: <#T##UIControlEvents#>)
        
    }
    
    
    //设置所有子控制器
        private func setupChildViewControllers(){
        let array = [ ["claName":"WBHomeViewController","title":"首页","imageName":"home"],["claName":"WBMessageViewController","title":"消息","imageName":"message_center"],
                      ["claName":"UIViewController"],
                      ["claName":"WBDiscoverViewController","title":"首页","imageName":"discover"],["claName":"WbProfileViewController","title":"首页","imageName":"profile"],
                      ];
        //定义一个数组
        var arrayM = [UIViewController]()
        
        for dict in array{
            //controller()是调用下面extension中的方法
            arrayM.append(controller(dic: dict))
        }
        viewControllers = arrayM
    }
    
    //使用字典创建一个子控制器
    private func controller(dic: [String: String]) -> UIViewController{
        //取字典内容
        guard let claName = dic["claName"],
            let title = dic["title"],
            let imageName = dic["imageName"],
            let cls = NSClassFromString(Bundle.main.nameSpace2 + "." + claName) as? UIViewController.Type
            else{
                //这里为空,为空后直接返回，退出这个函数
                return UIViewController()
        }
        
        //创建视图控制器
        let vc = cls.init()
        vc.title = title
        vc.tabBarItem.image = UIImage(named: "tabbar_" + imageName)
        vc.tabBarItem.selectedImage = UIImage(named: "tabbar_" + imageName + "_selected")?.withRenderingMode(.alwaysOriginal)
        
        //设置tabbar标题字体  默认是12号字体
        //.normal好像不起作用，用UIControlState(rawValue: 0)代替
        vc.tabBarItem.setTitleTextAttributes([NSFontAttributeName :UIFont.systemFont(ofSize: 15),NSForegroundColorAttributeName:UIColor.black], for:UIControlState(rawValue: 0))
        vc.tabBarItem.setTitleTextAttributes([NSFontAttributeName :UIFont.systemFont(ofSize: 15),NSForegroundColorAttributeName:UIColor.orange], for:.selected)
        //实例化导航控制器的时候，会调用push方法，将rootVC压栈
        let nav = WBNavigationController(rootViewController: vc)
        return nav
    }

}

//extension类似与OC中的分类，swift中还可以用来切分代码块
//可以把相近功能的函数，放在一个extension中
//便于代码维护
//注意：和oc的分类一样，extension中不能定义属性，只能定义方法
extension WBMainViewController{
    

}
